from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from meiduo_admin.serializer import sku_serializer
from meiduo_admin.my_paginate import MyPageNumberPagination


class SKUTableView(ModelViewSet):
    pagination_class = MyPageNumberPagination

    serializer_class = sku_serializer.SKUTableSerializer
    # queryset = SKU.objects.all()
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return SKU.objects.filter(name__contains=keyword)
        else:
            return SKU.objects.all()





class SKUCategoryView(ListAPIView):
    serializer_class = sku_serializer.SKUCategorySerializer
    queryset = GoodsCategory.objects.filter(subs=None)


class GoodSimpleView(ListAPIView):
    serializer_class = sku_serializer.GoodSimpleSerializer
    queryset = SPU.objects.all()


class SPUSpecsView(ListAPIView):
    serializer_class = sku_serializer.SPUSpecsSerializer
    # queryset = SPUSpecification.objects.all()

    def get_queryset(self):
        pk = self.kwargs.get('pk')

        return SPUSpecification.objects.filter(spu_id=pk)